<!doctype html>
<html xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>文档地址管理</title>
<link rel="stylesheet" type="text/css" href="../zui/css/zui.min.css">
</head>

<body>
	<div id="app">
		<div class="title-info">
			<div class="alert alert-primary">
				<div class="content">Tips：保存的swagger-resources实际会自动获取所有文档到“详细地址管理”里，所以这里的地址无实际用处，实际起作用的是“详细地址管理”里的哦，所以这里面只保留查询列表了，不再提供增删改，想要添加修改就去“详细地址管理”里操作吧！</div>
			</div>
		</div>
		<table class="table table-bordered setting-table">
			<thead>
				<tr>
					<td style="width: 50px;">序号</td>
					<td>地址</td>
					<td>重写域名地址</td>
					<td>持久化时间</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
				<tr v-for="(item,index) in swaggerResourcesList" :key="item.id" :data-id="item.id" :data-index="index" >
					<td>{{index+1}}</td>
					<td>{{item.url}}</td>
					<td>{{item.rewriteDomainUrl}}</td>
					<td>{{item.lastSync}}</td>
					<td>
<!--						<button class="btn btn-danger" type="button" v-on:click="deleteDocUrl($event)">删除</button>-->
<!--						<button class="btn btn-info" type="button" v-on:click="editDocUrl($event)">编辑</button>-->
						<!--<button class="btn btn-danger" type="button" v-on:click="syncDocData($event)">持久化</button>-->
					</td>
				</tr>
				<tr>
					<td colspan="5" align="center">
						<button class="btn" type="button" v-on:click="btnRefreshList">　刷新　</button>
						<button class="btn btn-info" type="button" v-on:click="exportDocument">导出文档</button>
<!--						<button class="btn btn-primary" type="button" v-on:click="addNewDocument">增加文档</button>-->
					</td>
				</tr>
			</tbody>
		</table>
		<!-- 增加文档弹出框 -->
		<div class="modal fade" id="addNewDocumentModal">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">
							<span aria-hidden="true">×</span><span class="sr-only">关闭</span>
						</button>
						<h4 class="modal-title">输入文档地址</h4>
					</div>
					<div class="modal-body">
						<div class="input-line">
							地址：
							<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例：http://127.0.0.1/swagger-resources 或 http://127.0.0.1/v2/api-docs">
						</div>
						<div class="input-line">
							重写域名地址：
							<input v-model="rewriteDomainUrl" type="text" name="rewriteDomainUrl" class="form-control" placeholder="文档展示配置页 勾选“重写域名”重写的地址">
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-primary" v-on:click="addNewDocumentBtn">保存</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="../mg-ui/js/jquery-3.1.0.min.js"></script>
<script src="../zui/js/zui.min.js"></script>
<script src="../mg-ui/js/common.js"></script>
<script src="../mg-ui/js/mg-ui-cache-keys.js"></script>
<script src="../vue/vue.js"></script>
<script src="../mg-ui/js/toast.js"></script>

<script>
	var urlBase = "../../";
	var app = new Vue({
		el: '#app',
		data: {
			swaggerResourcesList: [],
			addNewDocumentInput: '',
			rewriteDomainUrl: '',
			oldUrl: ''
		},
		methods: {
			btnRefreshList: function(){
				this.refreshList();
				Toast.success("刷新成功！");
			},
			refreshList: function(){
				getStorage(cacheKeys.swaggerResourcesList, function(data){
					//console.log(data);
					app.swaggerResourcesList = data;
				});
			},
			editDocUrl: function(event){
				var tr = $(event.currentTarget).parents("tr");
				var index = tr.data("index");
				var item = app.swaggerResourcesList[index];
				app.oldUrl = item.url;
				app.addNewDocumentInput = item.url;
				app.rewriteDomainUrl = item.rewriteDomainUrl;
				$('#addNewDocumentModal').modal({moveable:true});
			},
			addNewDocument: function(){
				app.oldUrl = '';
				app.addNewDocumentInput = '';
				$('#addNewDocumentModal').modal({moveable:true});
			},
			addNewDocumentBtn: function(){
				var addNewDocumentInput = app.addNewDocumentInput;
				if(isEmpty(addNewDocumentInput)) {
					Toast.error("地址不可以为空");return;
				}
				var param = {
					resourcesUrl: addNewDocumentInput,
					rewriteDomainUrl: app.rewriteDomainUrl,
					oldUrl: app.oldUrl
				};
				ajaxTemp(urlBase + "swagger-mg-ui/document/addSwaggerResources", "post", "json", param, function(json){
					if(validateResult(json)) {
						//window.parent.document.location.reload();
						//app.swaggerResourcesList.push(addNewDocumentInput);
						app.refreshList();
						$('#addNewDocumentModal').modal('hide');
						Toast.success("保存成功，刷新后生效！");
					}
				});
			},
			exportDocument: function(){
				getExport().exportDocument();
			},
			deleteDocUrl: function(event){
				if(!confirm("确定要删除吗？")) {
					return;
				}
				var tr = $(event.currentTarget).parents("tr");
				var index = tr.data("index");
				var newDocList = [];
				for(var i=0;i<app.swaggerResourcesList.length;i++){
					if(i != index) {
						newDocList.push(app.swaggerResourcesList[i]);
					}
				}
				setStorage(cacheKeys.swaggerResourcesList, newDocList, function(){
					app.swaggerResourcesList = newDocList;
				});
			},
			syncDocData: function(event){
				var tr = $(event.currentTarget).parents("tr");
				var index = tr.data("index");
				var newDocUrl = app.swaggerResourcesList[index].url;
				ajaxTemp(urlBase + "swagger-mg-ui/document/syncDocData", "post", "json", {resourcesUrl: newDocUrl}, function(json){
					if(validateResult(json)) {
						app.refreshList();
						Toast.success("持久化成功！");
					}
				});
			}
		},
		mounted: function(){
			this.refreshList();
		},
		watch: {
		}
	});
</script>

<style>
	#app{padding-top: 10px;}
	.input-line{margin-bottom: 15px;}
</style>
</html>



